package edu.cuit.avatar.concurrent;

/**
 * @author <a href="mailto:1020zhaodan@163.com">Adan</a>
 * @version 1.0
 * @date 2025/7/17 15:08
 */
public class BuyCigarettes {
    public static void main(String[] args) {
        new Father().start();
    }

    private static class Father extends Thread{
        @Override
        public void run() {
            try {
                System.out.println("爸爸想抽烟，发现没有了");
                System.out.println("叫来儿子，给你100块买包中华");
                Thread son = new Son();
                son.start();
                //老爸想抽烟，需要等待儿子买烟回来（儿子线程执行完毕）
                son.join();
                System.out.println("爸爸接过烟，对你把头点");
            } catch (InterruptedException e) {
                System.out.println("儿子去哪了");
                throw new RuntimeException(e);
            }
        }
    }

    private static class Son extends Thread{
        @Override
        public void run() {
            try {
                System.out.println("接过老爸的人民币，屁颠颠出门了");
                System.out.println("发现路边有洗脚城，做个大宝剑:10秒钟");
                for (int i = 0; i < 10; i++) {
                    System.out.println(i + "秒过去了");
                    Thread.sleep(1000);
                }
                System.out.println("赶紧买烟去");
                System.out.println("手拿一包中华回家了");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
